<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admin extends CI_Controller {

	function __construct(){
            parent::__construct();
            $this->load->library(array('session','form_validation'));
            $this->load->helper(array('form', 'url'));
            $this->admin_id = $this->session->userdata('admin_id');
            $this->admin_username = $this->session->userdata('admin_username');
            $this->load->model('fakultas_model');
            $this->load->model('jurusan_model');
            $this->load->model('admin_model');
            $this->load->model('skim_model');
            $this->load->model('program_model');
            $this->load->model('penelitian_model');
            $this->load->model('status_penelitian_model');
        }
	
        public function authorize()
        {
            if($this->admin_id && $this->admin_model->get_admin($this->admin_id)->num_rows() > 0)return true;
            else
            {
                redirect('admin/login');
                return false;
            }
        }
        
	public function index()
	{
            $result = $this->authorize();
            if($result == false)return;
//            $this->load->view('template/header'); 
//            $this->load->view('admin/admin');
//            $this->load->view('admin/sidebar');
//            $this->load->view('template/footer');
            redirect("admin/detail");
	}
        
        public function detail(){
            if($this->admin_id)
            {
                $petunjuk = "Selamat datang di SIM Penelitian ITS. Selamat melakukan penelitian.</br> </br> Salam LPPM ITS";
                $data["data_petunjuk"] = $petunjuk;
                $data["data_admin"] = $this->admin_model->get_admin($this->admin_id);
                $this->load->view('template/header', $data);
                $this->load->view('admin/detail', $data);
                $this->load->view('admin/sidebar', $data);
                $this->load->view('template/footer', $data);
            }
            else{
                redirect("admin");
            }
        }
        
        public function login()
        {
            $email = $this->input->post('email');
            $pass = $this->input->post('pass');

            $this->form_validation->set_message('required', "%s harus diisi");
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
            $this->form_validation->set_rules('pass', 'Password', 'trim|required');

            if ($this->form_validation->run() == FALSE)
            {
                $data["ket"] = ""; 
                $this->load->view('template/header'); 
                $this->load->view('admin/login',$data);
                $this->load->view('template/footer');
            }
            else
            {
                $data_admin = $this->admin_model->cek_login($email, $pass);
                if($data_admin){
                    $this->session->set_userdata('admin_id',$data_admin->ADMIN_ID);
                    $this->session->set_userdata('admin_username',$data_admin->ADMIN_USERNAME);
                    $data["ket"] = "sukses login $data_admin->ADMIN_USERNAME";
                    redirect('admin');
                }
                else {
                    $data["ket"] = "username password tidak valid";
                    $this->load->view('template/header', $data);
                    $this->load->view('admin/login', $data);
                    $this->load->view('template/footer', $data);
                }
            }
        }
        
        public function logout()
        {
            $this->session->unset_userdata('admin_id');
            $this->session->unset_userdata('admin_username');
            redirect("admin");
        }
        
        public function view_fak_jur()
        {
            if($this->admin_id)
            {
                $data['list_fakultas'] = $this->fakultas_model->get_fakultas();
                $data['list_jurusan' ] = $this->jurusan_model->get_jurusan_fakultas();

                $this->load->view('template/header', $data); 
                $this->load->view('admin/view_fak_jur', $data);
                $this->load->view('admin/sidebar', $data);
                $this->load->view('template/footer', $data);
            }
            else
            {
                redirect('admin');
            }
        }
        
        public function register()
        {
            if($this->admin_id)
            {
                $username = $this->input->post('username');
                $pass = $this->input->post('pass');
                $email = $this->input->post('email');
                $nohp = $this->input->post('nohp');
                $alamat = $this->input->post('alamat');

                $data['submit_data']=array(
                    'username' => $this->input->post('username'),
                    'email' => $this->input->post('email'),
                    'nohp' => $this->input->post('nohp'),
                    'alamat' => $this->input->post('alamat'),
                );

                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
                $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

                $this->form_validation->set_rules('username', 'Username', 'trim|required');
                $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
                $this->form_validation->set_rules('nohp', 'Nomor Hp', 'trim|required|numeric');
                $this->form_validation->set_rules('alamat', 'Alamat', 'trim|required');
                $this->form_validation->set_rules('pass', 'Password', 'trim|required');

                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data);
                    $this->load->view('admin/register', $data);
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->admin_model->insert_admin($username, $email,$nohp,$alamat,$pass,1,"default.jpg");
                    $last_id = $this->db->insert_id();

                    $this->load->library('upload');  
                    $this->load->library('image_lib'); 
                    $config['upload_path'] = './static/images/admin/photo'; 
                    $config['allowed_types'] = '*';  
                    $config['max_size']  = '0';
                    $config['file_name'] = $last_id;
                    $config['max_width']  = '0';  
                    $config['max_height']  = '0';  
                    $config['overwrite']  = TRUE;
                    $this->upload->initialize($config);
                    $upload = $this->upload->do_upload();

                    if($upload)
                    {
                        $data = $this->upload->data();
                        $path = $data['file_name'];
                        $this->admin_model->insert_path_foto($path,$last_id);
                    }

                    $this->load->view('template/header', $data);
                    $this->load->view('admin/register_finished', $data);
                    $this->load->view('template/footer', $data);
                }
            }
            else
            {
                redirect('admin');
            }
        }
        
        public function edit()
        {
            if($this->admin_id)
            {
                
                $username = $this->input->post('username');
                $email = $this->input->post('email');
                $nohp = $this->input->post('nohp');
                $alamat = $this->input->post('alamat');

                $data['data_admin']=array(
                    'username' => $this->input->post('username'),
                    'email' => $this->input->post('email'),
                    'nohp' => $this->input->post('nohp'),
                    'alamat' => $this->input->post('alamat'),
                );
                $data["data_admin"] = $this->admin_model->get_admin($this->admin_id);
                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
                $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

                $this->form_validation->set_rules('username', 'Username', 'trim|required');
                $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
                $this->form_validation->set_rules('nohp', 'Nomor Hp', 'trim|required|numeric');
                $this->form_validation->set_rules('alamat', 'Alamat', 'trim|required');
                $this->form_validation->set_rules('pass', 'Password', 'trim|required');

                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data);
                    $this->load->view('admin/edit', $data);
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->admin_model->update_admin($admin_id, $username, $email,$nohp,$alamat,1);

                    $this->load->library('upload');  
                    $this->load->library('image_lib'); 
                    $config['upload_path'] = './static/images/admin/photo'; 
                    $config['allowed_types'] = '*';  
                    $config['max_size']  = '0';
                    $config['file_name'] = $last_id;
                    $config['max_width']  = '0';  
                    $config['max_height']  = '0';  
                    $config['overwrite']  = TRUE;
                    $this->upload->initialize($config);
                    $upload = $this->upload->do_upload();

                    if($upload)
                    {
                        $data = $this->upload->data();
                        $path = $data['file_name'];
                        $this->admin_model->insert_path_foto($path,$admin_id);
                    }

                    $this->load->view('template/header', $data);
                    $this->load->view('admin/detail', $data);
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer', $data);
                }
            }
            else
            {
                redirect('admin');
            }
        }
        
        public function edit_pass(){
            if($this->admin_id)
            {
                
                $email = $this->input->post('email');
                $passold = $this->input->post('passold');
                $passnew = $this->input->post('passnew');
                
                $data['submit_data']=array(
                    'email' => $this->input->post('email'),
                    'passold' => $this->input->post('passold'),
                    'passnew' => $this->input->post('passnew')
                );

                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");
                $this->form_validation->set_rules('email', 'Email', 'trim|required');
                $this->form_validation->set_rules('passold', 'Password Lama', 'required');
                $this->form_validation->set_rules('passnew', 'Password Baru', 'trim|required');
                
                $petunjuk = "Halaman ini adalah halaman untuk mengganti password.</br> </br> Jangan sampai anda lupa dengan password anda";
                $data["data_petunjuk"] = $petunjuk;
                
                
                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data);
                    $this->load->view('admin/editpass', $data);
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->admin_model->change_pwd($this->admin_id, $email, $passold, $passnew);
                    $this->load->view('template/header', $data);
                    $this->load->view('admin/editpass', $data);
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer', $data);
                    redirect("admin");
                }
                
            }
            else{
                redirect("admin");
            }
        }
        
        public function list_proposal_baru(){
            if($this->admin_id)
            {
//                $skim_id = $this->input->post('skim_id');
//                
//                $this->form_validation->set_message('required', "Anda wajib memilih %s sebelum melanjutkan pencarian");
//                $this->form_validation->set_rules('skim_id','SKIM','trim|required');
//                
//                if ($this->form_validation->run() == FALSE)
//                {
//                    $petunjuk = "Pilih Program, lalu pilih SKIM sebelum mendaftarkan data penelitian lebih lanjut";
//                    $data["data_petunjuk"] = $petunjuk;
//                    $data["data_skim"] = $this->skim_model->get_skim();
//                    $data["data_program"] = $this->program_model->get_program();
//                    $data["data_tahun"] = $this->program_model->get_list_tahun();
//                    
//                    $this->load->view('template/header'); 
//                    $this->load->view('penelitian/cari_penelitian',$data);
//                    $this->load->view('admin/sidebar', $data);
//                    $this->load->view('template/footer');
//                }
//                else
//                {
//                  redirect("admin/detail");
                    $petunjuk = "Halam hasil pencarian penelitian";
                    $data["data_petunjuk"] = $petunjuk;
                    $data["list_penelitian"] = $this->penelitian_model->get_proposal_baru();
                    $data["list_statuspen"] = $this->status_penelitian_model->get_statuspen();
                    $this->load->view('template/header'); 
                    $this->load->view('penelitian/list_penelitian_baru',$data);
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer');
//                }
                
            }
            else{
                redirect("admin");
            }
        }
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */